﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Input;
using System.Windows.Media.Animation;
using System.Threading;
namespace GoL_Silverlight
{
	public partial class MainPage : UserControl
	{
		int j, a, p = 10, old;
		GolSim active;
		ScaleTransform scale = new ScaleTransform { ScaleX = 0.2, ScaleY = 0.2 };
		Thread t;
		public MainPage()
		{
			InitializeComponent();
			Add("", "Empty");
			Add("fBp8GBp8Dg8WFyQlfA0RFhckJXwCAwwSFhd8AgMMEBITGBp8DBIafA0RfA4PfA==", "Gosper glider gun");
            Add("NUBBfDQ2QEF8IyQ0NTd8IyU0NTc4PD18Hh8mNDU3PD18GhsdICMmNDZ8GhseHyYvNUBBQnwjJS0vQEFCfCMkLi8/Q3x8Kj4/Q0R8KissfC18LC02fDd8NTY3QkNEfD9AfC4vMD9AfB0eLS4wMT4/fB0fLS4wMT9BfBEeHyAtLi8wMUBBfA4PEBEVFh8gISgpLC0xMnwNDg8QFBUeHyAoKXwGBw0QFBUXGRodH0BBRUZ8BQgNDg8QFRcZGh0eMjo7QEFFRnwFDg8QERUXGDEzNDk6O3wFETE5OjxCQ0R8BQc6OzxCQ0R8BQcxMjQ7Q3wGHy8yfCAhLzF8HyB8AwQICXwDCXxCQ3wECDAxMkJDfAUGBy8zfCYoLjR8JygvM3wLDCcwMTI7PHwMMDEyOzw9fAwOFTo8PXwNDhMVMzQ6Ozx8CRESJSYzO3wHCRESJCguNDU2fAgJERIjKS8wNnwTFRojJyouL3wEFRkjfAMEBRkaGx4kKCx8AgMEBQYlJiorLXwBAgYHEC18AgMEBQYRLS58AgYPEBE1N0BBSEl8AwU2Nz5DRkt8BCorNj5DRkt8HB0pKissOj5DRkt8GhsdHikqLC07QEFISXwEBRobHB0rLDk6O3wEBRscfAkTFD18CAoSExQVPj98AQIHCQoQERIVGBo9PnwBAgYHCQoUFRgbfAcJChEbHCMkMjM0NTY3OEJ8CAoQGR0eIyQwMTI0NTY4OTpAQnwJEBscLzQ1NjtBQnwYG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S80Q0RGSUtMTlF8DA8REhQXGRopMDEyNDU6O0JDRk1QUXwMDRAXGhsiIygpKywyNTk7QkNGSk18EBMXGhsiJCgrLTU2OUNFR0hJSktMTVVWfAcIEBESExQVFhgaJCcoLTU2Nzg5O0hJSktNVVd8BggQEhMUFSIkJSYnKC0uNTk6O0RGV3wGFxkiIyQoNDs8UFFWV1laXHwBAwQGBwwNISIpNDU6OzxBQ1BSVllbXHwBAgQHCw0aHCEiIygpNDs8QENRUld8BgsMGh0hIik1OTo7QUNJUlRVfAgJCxQaHCIjJCgvMDU2Nzg5O0hKUFFVWHwFCAwNExUiJCUmJygwNTY5R0tQU1RVWHwFCAkKDRIWJCcoMDI1OTtISlBVVnwHCA0TFSIkKCsxMjQ1OjtCSVBTVFVYfAUICQoNFBsiIygpKywtNEJEUFFVWHwFCAwNGRspLjQ2QkNSVFV8CAkLGhsnKS0uNTZGR1FSV3wGCwwWFycoR1BSVllbXHwBAgQHCw0WREVGUFFWV1laXHwBAwQGBwwNFxgZPERXfAYZITw+VVd8BggfITw9VVZ8BwggIXx8fDZ8JzY4fCUnNjdBQnwmJz58PT9CQ3wfIDs9P0BBRHweIDA7Pj9AQUJDfB4tMDI+P0FCfB0eKy0wMT0+P0BBQkV8LC08P0BBQ0V8PT5BQ3xCfD4/fDN8MTN8LS4yM3wtLnw=", "P192 Gun");
		}
        void Add(string s, string t)
        {
			bool[,] coords = new bool[100, 100];
			Byte[] bs = Convert.FromBase64String(s);
			j = 0;
            foreach (Byte b in bs)
            {
                if (((int)b) == 124)
                    j++;
                else
                    coords[((int)b), j] = true;
            }
			GolSim g = new GolSim(coords);
			g.RenderTransform = scale;
			g.MouseLeftButtonDown += new MouseButtonEventHandler(Click);
			cMargin(g, 20, p + (a * 110));
			m.Children.Add(g);
			TextBlock tb = new TextBlock();
			tb.Text = t;
			cMargin(tb, 122, p + (a * 110) + 3);
			m.Children.Add(tb);
			a++;
        }
		void Click(object s, MouseButtonEventArgs e)
		{
			btn.IsEnabled = true;
			GolSim g = (GolSim)s;
			if (g != active)
			{
				if (active != null)
				{
					togglesim(false);
					active.RenderTransform = scale;
					active.a = false;
					cMargin(active, 20, old);
				}
				g.RenderTransform = null;
				old = (int)Canvas.GetLeft(g);
				cMargin(g, 200, 10);
				g.a = true;
				active = g;
			}
		}
		void bclick(object sender, RoutedEventArgs e)
		{
			if (active != null)
			{
				if (active.tmr.IsEnabled)
				{
					togglesim(false);
				}
				else
				{
					togglesim(true);
				}
				
			}
		}
		void cMargin(UIElement e, int top, int left)
		{
			Canvas.SetTop(e, top);
			Canvas.SetLeft(e, left);
		}
		void togglesim(bool start)
		{
			if (start)
			{
				active.a = false;
				active.tmr.Start();
				btn.Content = "Stop simulation";
			}
			else
			{
				active.tmr.Stop();
				active.a = true;
				btn.Content = "Start simulation";
			}
		}
	}
}